package ru.royalgatemagazine.app.show.page;

import ru.royalgatemagazine.app.Debug;
import ru.royalgatemagazine.app.DialogGenerator;
import ru.royalgatemagazine.app.DialogGenerator.DialogGeneratorListener;
import ru.royalgatemagazine.app.R;
import ru.royalgatemagazine.app.http.HttpSimpleClient;
import ru.royalgatemagazine.app.show.about.AboutActivity;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class PageActivity extends Activity implements PageController, DialogGeneratorListener{
	
//	private boolean mMapVisible = false;
	private MWebView mWeb;
	private WebView mWebMap;
	private int mMid;
	private int mPage;
	private int mPages;
	private TextView mHeadText;
	private Dialog mDialog;
	
	public static final String INTENT_EXTRA_MID = "mid";
	public static final String INTENT_EXTRA_PAGE = "page";
	public static final String INTENT_EXTRA_PAGES = "pages";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		
		mMid = bundle.getInt(INTENT_EXTRA_MID);
		mPage = bundle.getInt(INTENT_EXTRA_PAGE);
		mPages = bundle.getInt(INTENT_EXTRA_PAGES);
		
		setContentView(R.layout.page);
		mHeadText = (TextView) findViewById(R.id.text);
		mWeb = (MWebView) findViewById(R.id.web_content);
		mWeb.setListener(this);
		mWeb.getSettings().setUseWideViewPort(true);
		mWebMap = (WebView) findViewById(R.id.web_map);
		mWeb.getSettings().setJavaScriptEnabled(true);
		mWeb.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				dismissDialog();
			}
		});
		
		String text = getString(R.string.loading);
		mDialog = DialogGenerator.createProgressTitledDialog(PageActivity.this, PageActivity.this, DialogGenerator.DIALOG_PROGRESS, null, text);
		
		mWebMap.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Debug.log("shouldOverrideUrlLoading():url:"+url);
				String patern = "js-call:loadMapPage:"; 
				if(url != null && url.indexOf(patern)!=-1){
					boolean result = false;
					String num = url.substring(patern.length());
					try{
						int page = Integer.parseInt(num);
						onPageClick(page);
						result = true;
					}
					catch(Exception e){
						Debug.log(String.format("Integer.parseInt(%s) is fail!", num));
						result = false;
					}
					return result;
				}
				return false;
			}
		});
		prepTitle();
	}
	
	public void pageLeft() {
		Debug.log("pageLeft()");
		onPageClick(mPage-1);
	}

	public void pageRight() {
		onPageClick(mPage+1);
		Debug.log("pageRight()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		redrawHeaderPages(mPage, mPages);
		showDialog();
		mWeb.loadUrl(String.format(HttpSimpleClient.PAGE_LINK_PATTENR, mMid, mPage));
		mWebMap.loadUrl(String.format(HttpSimpleClient.MAP_LINK_PATTENR, mMid));
	}
	
	private void prepTitle(){
		findViewById(R.id.button1).setOnClickListener(onCatalogClicker);
		findViewById(R.id.button2).setOnClickListener(onMapClicker);
		findViewById(R.id.pic).setOnClickListener(onPicClicker);
	}
	
	public OnClickListener onCatalogClicker = new OnClickListener() {
		@Override
		public void onClick(View v) {
			onHeaderButtonCatalogClick();
		}
	};
	
	public OnClickListener onMapClicker = new OnClickListener() {
		@Override
		public void onClick(View v) {
			onHeaderButtonMapClick();
		}
	};
	
	public OnClickListener onPicClicker = new OnClickListener() {
		@Override
		public void onClick(View v) {
			onHeaderButtonPicClick();
		}
	};
	
	
	@Override
	public void onHeaderButtonCatalogClick() {
		finish();
	}

	protected void onHeaderButtonPicClick() {
		Intent i = new Intent(this, AboutActivity.class);
		startActivity(i);
	}

	@Override
	public void onHeaderButtonMapClick() {
		boolean mMapVisible = mWebMap.getVisibility()==View.VISIBLE;
		if(mMapVisible){
			mWebMap.setVisibility(View.GONE);
//			mMapVisible = false;
		}
		else{
//			mMapVisible = true;
			mWebMap.setVisibility(View.VISIBLE);
			mWebMap.loadUrl(String.format(HttpSimpleClient.MAP_LINK_PATTENR, mMid));
		}
	}

	@Override
	public void onPageClick(int page) {
		if(1<=page && page<=mPages){
			mPage = page;
			showDialog();
			mWeb.loadUrl(String.format(HttpSimpleClient.PAGE_LINK_PATTENR, mMid, mPage));
			redrawHeaderPages(mPage, mPages);
		}
	}
	
	private void redrawHeaderPages(int page, int pages){
		mHeadText.setText(String.format("%d/%d", mPage, mPages));
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		Debug.log(String.format("PageActivity:onConfigurationChanged:mMid=%d, mPage=%d", mMid, mPage));
	}

	@Override
	public void hideMap() {
//		if(mMapVisible){
			mWebMap.setVisibility(View.GONE);
//			mMapVisible = false;
//		}
	}
	
	private void showDialog(){
		try{
			if(mDialog!=null && !mDialog.isShowing()){
				mDialog.show();
			}
		}
		catch(Exception e){
			Debug.log("try to show dialog" + e.toString());
		}
	}
	
	private void dismissDialog(){
		try{
			if(mDialog!=null && mDialog.isShowing()){
				mDialog.dismiss();
			}
		}
		catch(Exception e){
			Debug.log("try to dismiss dialog" + e.toString());
		}
	}

	@Override
	public void onClick(DialogInterface arg0, int arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		// TODO Auto-generated method stub
	}
}
